﻿using System;
using System.Collections;
using System.Linq;
using Deanery.Controller.Directories;

namespace Deanery.Controller.PersonData
{
    class DocumentController
    {
        private readonly deaneryEntities _db = new deaneryEntities();
        //not correct
        public void Insert(ArrayList reccord, Guid person)
        {
            var typeDoc = new TypeDocumentsController();

            var obj = new Document
                {
                    id_documents = Guid.NewGuid(),
                    id_type_documents = typeDoc.FindGuidByName(reccord[0].ToString()),
                    nv_number_document = reccord[1].ToString(),
                    nv_series_document = reccord[2].ToString(),
                    id_person = person
                };

            if (3 < reccord.Count) obj.nv_date_of_issue = reccord[3].ToString();
            if (4 < reccord.Count) obj.nv_issued = reccord[4].ToString();
            if (5 < reccord.Count) obj.nv_validity = reccord[5].ToString();

            _db.Documents.AddObject(obj);
            _db.SaveChanges();

        }


        public void Insert(Guid type, string series, string number, string data, string issued, Guid person)
        {
            var typeDoc = new TypeDocumentsController();

            var obj = new Document
            {
                id_documents = Guid.NewGuid(),
                id_type_documents = type,
                nv_number_document = number,
                nv_series_document = series,
                nv_date_of_issue = data,
                nv_issued = issued,
                id_person = person
            };

            _db.Documents.AddObject(obj);
            _db.SaveChanges();

        }

        public Guid FindGuidByKeyFields(Guid type, string series, string number, string data, string issued)
        {
            var obj = (from t in _db.Documents
                       where
                           t.id_type_documents == type && t.nv_series_document == series &&
                           t.nv_number_document == number && t.nv_date_of_issue == data &&
                           t.nv_issued == issued
                       select t.id_documents).SingleOrDefault();
            return obj;
        }

    }
}
